// pages/houseNext/houseNext.js
var app = getApp();
Page({

  /**
   * 页面的初始数据
   */
  data: {
    isAgree: false,
    userInfo: '',
    title2: '',
    canSubmit: false,
    isChange: false,
    special: [{ name: '地铁房', ischecked: false }, { name: '配套成型', ischecked: false }, {
      name: '急售',ischecked: false
    }, { name: '品质小区', ischecked: false }, { name: '交通便利', ischecked: false }, { name: '采光好', ischecked: false }, { name: '南北通透', ischecked: false }, { name: '环境优美', ischecked: false }, { name: '繁华地段', ischecked: false }, { name: '房型正', ischecked: false }, { name: '安全性高', ischecked: false }, { name: '随时看房', ischecked: false }, { name: '品牌地段', ischecked: false }, { name: '一梯两户', ischecked: false }, { name: '学区房', ischecked: false }, { name: '低密度', ischecked: false }, { name: '全南户型', ischecked: false }, { name: '小户型', ischecked: false }, { name: '无营业税', ischecked: false }, { name: '得房率高', ischecked: false }, { name: '婚房', ischecked: false }, { name: '近地铁', ischecked: false}],
    submitData: {
      contact: '',	//	联系人		
      phone: '',	//	手机号			
      title: '',	//	标题			
      housingDescription: '',	//	房源描述			
      housingCharacteristics: '',	//	房源特色			
      // rentalSaleStatus: '' 	//	出售状态	
    },
    formData:'',
    isRent: ''
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    if (options.isRent){
      this.setData({
        isRent: options.isRent
      })
    }
    this.setData({
      formData: wx.getStorageSync("house"),
      title2: options.title2,
      userInfo: JSON.parse(wx.getStorageSync('userInfo')),
      ['submitData.phone']: JSON.parse(wx.getStorageSync('userInfo')).phone
    })
    
  },
  bindInputFunc: function (e) {
    let key = e.currentTarget.dataset.key;
    this.data.submitData[key] = e.detail.value;
    this.setData({ submitData: this.data.submitData });
    this.changeValue();
  },
  // 房源特色
  clickFunc: function(e){
    var index = e.currentTarget.dataset.index;
    var selectedLen = 0;
    if (this.data.special[index].ischecked){
      this.data.special[index].ischecked = false
    }
    else{
      this.data.special.forEach(function (ele) {
        if (ele.ischecked == true) {
          selectedLen++
        }
      })
      if (selectedLen >= 3) {
        wx.showToast({
          title: '最多只能选择三个',
          icon: 'none',
          duration: 2000
        })
      }
      else {
        this.data.special[index].ischecked = !this.data.special[index].ischecked;
      }
    }
    this.setData({
      special: this.data.special
    })
  },
  // 修改手机号
  changeTelFunc: function(){
    this.setData({
      isChange: true
    })
  },
  // 取消修改
  canaelTelFunc: function(){
    this.setData({
      isChange: false
    })
  },
  changeValue: function () {
    var flag = true;
    var item = this.data.submitData;
    if ((item.name == '') || (item.contact == '') || (item.phone == '') || (item.title == '') || (item.housingDescription == '' || this.data.isAgree == false)) {
      flag = false;
    }
    this.setData({
      canSubmit: flag
    })
  },
  changeTextFunc: function (e) {//其他需求textarea值改变事件

    if (e.detail.value == 'false') {
      this.data.isAgree = true;
    } else {
      this.data.isAgree = false;
    }

    this.setData({
      isAgree: this.data.isAgree
    })
    this.changeValue();
  },
  changeTextarea: function (e) {
    this.data.submitData.housingDescription = e.detail.value;
    this.setData({ submitData: this.data.submitData });
    this.changeValue();
  },
  
  submit: function (e) {
    var arr = [];
    for (var i = 0; i < this.data.special.length; i++){
      if(this.data.special[i].ischecked){
        arr.push(this.data.special[i].name)
      }
    }
    this.data.submitData.housingCharacteristics = arr.join(",");
    for(var key in this.data.submitData){
      this.data.formData[key] = this.data.submitData[key]
    }
    this.submitFunc()
  },
  openText: function (e) {
    wx.navigateTo({
      url: '/pages/text/text?type=3',
    })
  },
  submitFunc: function(){
    var _this = this;
    if (_this.data.isRent){
      wx.request({
        url: app.globalData.httpApiCar + 'tGyHouseRental/save',
        data: this.data.formData,
        method: 'POST',
        header: {
          'content-type': 'application/x-www-form-urlencoded' // 默认值
        },
        success: function (res) {
          if (res.data.success) {
            // wx.redirectTo({
            //   url: '/pages/regsuccess/regsuccess?btnText=返回首页&showText=发布成功'
            // })
            // wx.setStorage({
            //   key: "url",
            //   data: "/pages/houseRenting/houseRenting"
            // })
            wx.navigateBack({
              delta: 2
            })
          }
          else {
            wx.showToast({
              title: res.data.error,
              icon: 'none',
              duration: 2000
            })
          }
        }
      })
    }
    else{
      wx.request({
        url: app.globalData.httpApiCar + 'tGyHouseSale/save',
        data: this.data.formData,
        method: 'POST',
        header: {
          'content-type': 'application/x-www-form-urlencoded' // 默认值
        },
        success: function (res) {
          if (res.data.success) {
            wx.navigateBack({
              delta: 2
            })
          }
          else{
            wx.showModal({
              title: '提示',
              content: res.data.error,
              showCancel: false,
              success: function (res) {
                if (res.confirm) {
                  wx.navigateBack({
                    delta: 2
                  })
                }
              }
            })
          }
        }
      })
    }
  },
  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {
    // this.getAllType()
  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  }
})